// pages/chat/components/store-select/store-select.js
import $service from '../../../../service';
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    paymentMethod: {
      type: Number,
      value: 1
    },
    isAuto: {
      type: String,
      value: '0'
    },
    store: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    doctorStoreList: [],
    hisType: $service.isHis
  },

  /**
   * 组件的方法列表
   */
  lifetimes: {
    ready() {
      console.log('初始化')
      this.getDoctorStoreList('')
    },
  },
  methods: {
    toggle() {
      this.data.show = !this.data.show
      this.setData({
        show: this.data.show
      })
    },
    input(e) {
      let {value} = e.detail
      console.log(value)
      this.getDoctorStoreList(value)
    },
    focus() {
      console.log(this.properties.store.group_name)
      this.setData({
        show: true
      })
    },
    changeStore(e) {
      let {store} = e.currentTarget.dataset
      this.triggerEvent('changeStore', store)
      this.setData({
        show: false
      })
    },
    getDoctorStoreList(keyword) {
      if (this.properties.paymentMethod == 2 && this.properties.isAuto != 1) { // 医保药房
          this.setData({
              doctorStoreList: [{
                  group_name: this.data.hisType ? '院内药店' :'医保平台',
                  group_no: 0,
              }]
          })
      } else {
          app.http.getStoreList({keyword: keyword}).then(res => {
              if (this.data.hisType && this.properties.isAuto == '0') {
                this.data.doctorStoreList.push({
                  group_name: '院内药店',
                  group_no: 0
                })
              }
              this.setData({
                  doctorStoreList: [...this.data.doctorStoreList, ...res.data]
              })
          })
      }
  },
  }
})
